package com.example.role.web;


import com.example.role.auth.CookieUtil;
import com.example.role.auth.JWTUtil;
import com.example.role.pojo.User;
import com.example.role.service.IUserService;


import com.example.role.service.serviceimpl.UserServiceImpl;
import com.example.role.util.Const;
import com.example.role.util.ServerResponse;
import com.example.role.util.TableServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 职工表现层
 */
@RestController

public class UserController {
    @Autowired
    private UserServiceImpl userService;


    @RequestMapping("login")
    public ServerResponse login(User user, HttpServletResponse response) {
        System.out.println(user);
        return userService.login(user, response);
    }

    @RequestMapping("forget-password")
    public ServerResponse forgetPassword(User user) {
        return userService.forgetPassword(user);
    }

    @RequestMapping("update-password")
    public ServerResponse updatePassword(User user) {
        System.out.println(user);
        return userService.updatePassword(user);
    }

    @RequestMapping("get-user")
    public TableServerResponse getUser(Integer page, Integer limit){
        return userService.getUser(page,limit);
    }

    @RequestMapping("update-role")
    public ServerResponse updateRole(User user){
        System.out.println("user"+user);
        return userService.updateRole(user);
    }

    @RequestMapping("get-userinfo")
    public ServerResponse getUserinfo(@CookieValue(value = "token",required = false)String token){
        System.out.println(token);
        return userService.getUserinfo(token);
    }

    @RequestMapping("logout")
    public ServerResponse logout(HttpServletResponse response){
        CookieUtil.resposeCookie("",0,response);
        return ServerResponse.createSuccess("退出成功");
    }

    @RequestMapping("selectUserById")
    public ServerResponse selectUserById(Integer id){
        System.out.println(id);
        return userService.selectUserById(id);
    }

    @RequestMapping("selectUserByTokenId")
    public ServerResponse selectUserByTokenId(@CookieValue("token")String token){
        User user = JWTUtil.parserToken(token, Const.PUB_RSA_PATH);
        return userService.selectUserById(user.getUserId());
    }
    @RequestMapping("update-user")
    public ServerResponse updateUser(@CookieValue("token")String token, User user){
        System.out.println(token+user);
        return userService.updateUser(token,user);
    }

    @RequestMapping("add-user")
    public ServerResponse addUser(User user){
        return userService.addUser(user);
    }

    @RequestMapping("delete-user")
    public ServerResponse deleteUser(String ids){
        System.out.println(ids);
        return userService.deleteUser(ids);
    }
}
